package com.pzhu.dao.impl;
//JDBC���뵼��İ�
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.pzhu.dao.LoginDao;
import com.pzhu.pojo.User;

public class LoginDaoImpl implements LoginDao{
		@Override
		public User checkLoginDao(String uname, String pwd) {

			Connection conn=null;
			PreparedStatement ps=null;
			ResultSet rs=null;

			User u=null;
			
			try {

				Class.forName("com.mysql.cj.jdbc.Driver");

				conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javawebclass?useUnicode=true&characterEncoding=utf8","root","0714");

				String sql="select * from t_user where uname=? and pwd=?";

				ps=conn.prepareStatement(sql);

				ps.setString(1, uname);
				ps.setString(2, pwd);

				rs=ps.executeQuery();

				while(rs.next()){
					u=new User();
					u.setUid(rs.getInt("uid"));
					u.setUname(rs.getString("uname"));
					u.setPwd(rs.getString("pwd"));
				}
				
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally{

				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

			return u;
		}
		@Override
		public User checkUidLoginDao(String uid) {

			Connection conn=null;
			PreparedStatement ps=null;
			ResultSet rs=null;

			User u=null;
			
			try {

				Class.forName("com.mysql.cj.jdbc.Driver");

				conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javawebclass?useUnicode=true&characterEncoding=utf8","root","0714");

				String sql="select * from t_user where uid=?";
				ps=conn.prepareStatement(sql);

				ps.setString(1, uid);

				rs=ps.executeQuery();

				while(rs.next()){
					u=new User();
					u.setUid(rs.getInt("uid"));
					u.setUname(rs.getString("uname"));
					u.setPwd(rs.getString("pwd"));
				}


			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally{

				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

			return u;
		}
		}
